package com.keyue.common.exception;

import com.keyue.common.constant.ServerResult;
import com.keyue.common.constant.ServerResultCode;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>基础异常类，所有自定义异常类都需要继承本类</p>
 *
 * @author A80076
 * @date 2021/1/11
 */
@Slf4j
public class BusinessException extends RuntimeException {

    private static final long serialVersionUID = 3152549963899218489L;
    @Setter
    @Getter
    private String errorDesc;
    @Setter
    @Getter
    private Integer errorCode;
    @Setter
    @Getter
    private Object data;

    public BusinessException() {
        super(ServerResultCode.FAIL.getErrorDesc());
        this.errorCode = ServerResultCode.FAIL.getErrorCode();
        this.errorDesc = ServerResultCode.FAIL.getErrorDesc();
    }

    public BusinessException(int errorCode, String errorDesc) {
        super(errorDesc);
        this.errorCode = errorCode;
        this.errorDesc = errorDesc;
    }

    public BusinessException(ServerResultCode serverResultCode) {
        super(serverResultCode.getErrorDesc());
        this.errorCode = serverResultCode.getErrorCode();
        this.errorDesc = serverResultCode.getErrorDesc();
        log.error("业务异常: ", this);
    }

    public BusinessException(ErrorInfo errorInfo) {
        super(errorInfo.getErrorDesc());
        this.errorCode = errorInfo.getErrorCode();
        this.errorDesc = errorInfo.getErrorDesc();
        log.error("业务异常: ", this);
    }

    public BusinessException(ServerResult<?> serverResult) {
        super(serverResult == null ? ServerResultCode.FAIL.getErrorDesc() : serverResult.getMessage());
        if (serverResult == null) {
            this.errorCode = ServerResultCode.FAIL.getErrorCode();
            this.errorDesc = ServerResultCode.FAIL.getErrorDesc();
        } else {
            this.errorCode = serverResult.getResult();
            this.errorDesc = serverResult.getMessage();
            this.data = serverResult.getData();
        }
        log.error("业务异常: ", this);
    }

    public BusinessException(String message) {
        super(message);
    }

    public BusinessException(Throwable cause) {
        super(cause);
        log.error("业务异常: ", this);
    }

    public BusinessException(String message, Throwable cause) {
        super(message, cause);
        log.error("业务异常: ", this);
    }
}
